'use strict';

const Controller = require('egg').Controller;
const checkagent = require("../utils/checkagent");

class BookController extends Controller {
    // 增加
    async create(){
        try {
            const body = this.ctx.request.body;
            body.orderby = Number(body.orderby);
            await this.ctx.service.book.createBook(body);
            this.ctx.body = {
                code:20000,
                message:true
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                message: false
            }
        }
    }
    // 删除
    async destroy(){
        try {
            const id = this.ctx.params.id;
            await this.ctx.service.book.deleteBook(id);
            this.ctx.body = {
                code: 20000,
                message: true
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                message: false
            }
        }
    }
    // 修改
    async update(){
        try {
            const body = this.ctx.request.body;
            const id = this.ctx.params.id;
            await this.ctx.service.book.updateBook(id, body);
            this.ctx.body = {
                code: 20000,
                data: true
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                message: false
            }
        }
    }
    // 查询
    async index(){
        try {
            const query = this.ctx.request.query;
            const bookList = await this.ctx.service.book.getBookList(query);
            this.ctx.body = {
                code: 20000,
                message: true,
                data: bookList
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                message: false
            }
        }
    }
    async getBookPage(){  // 书籍列表页面
        const ua = checkagent(this.ctx.request.header["user-agent"]);
        let data = await this.ctx.service.website.getBookList();

        if(ua){
            await this.ctx.render("pc/book.html", data);
        }else{
            await this.ctx.render("phone/book.html", data);
        }

    }

}

module.exports = BookController;